// 定义一个创建面的函数
function createPolygon(position, color, opacity) {
    const positions = [
        Cesium.Cartesian3.fromDegrees(position.west, position.south),
        Cesium.Cartesian3.fromDegrees(position.west, position.north),
        Cesium.Cartesian3.fromDegrees(position.east, position.north),
        Cesium.Cartesian3.fromDegrees(position.east, position.south)
    ];
    const entity = viewer.entities.add({
        polygon: {
        hierarchy: positions,
        material: Cesium.Color.fromCssColorString(color || 'blue').withAlpha(opacity || 0.8),
        },
        polyline: {
        positions: positions.concat([positions[0]]),
        material: Cesium.Color.fromCssColorString(color || 'blue'),
        width: 3,
        },
    });
    positions.forEach(item => {
        viewer.entities.add({
        position: item,
        point: {
            material: Cesium.Color.fromCssColorString(color || 'blue'),
            pixelSize: 5,
            color: Cesium.Color.fromCssColorString(color || 'blue'),
        }
        });
    });
}

// 定义一个创建线的函数
function createPolyline(position, color, opacity) {
    const positions = [
        Cesium.Cartesian3.fromDegrees(position.west, position.south),
        Cesium.Cartesian3.fromDegrees(position.west, position.north),
        Cesium.Cartesian3.fromDegrees(position.east, position.north),
        Cesium.Cartesian3.fromDegrees(position.east, position.south)
    ];
    const entity = viewer.entities.add({
        polyline: new Cesium.PolylineGraphics({
        positions: positions.concat(positions[0]),
        material: Cesium.Color.fromCssColorString(color || 'blue').withAlpha(opacity || 0.8),
        width: 3,
        }),
    });
};

// 定义一个跳转到指定位置的函数
function flyToPosition(position, time, type) {
    let WE = 0
    let NS = 0
    if (type !== 'home') {
        WE = position.east - position.west
        NS = position.north - position.south
    }
    viewer.camera.flyTo({
        destination: Cesium.Rectangle.fromDegrees(
        position.west - WE,
        position.south - NS,
        position.east + WE,
        position.north + NS
        ),
        duration: time || 0.8
    })
};

// 定义一个 cartesian2 格式转为 cartesian3 格式的方法
function changeToThree(position) {
    if (!position) return [];
    return viewer.scene.globe.pick(viewer.camera.getPickRay(position), viewer.scene);
};

// 定义一个世界坐标转为经纬度的方法
function changeToLnglgt(position) {
    if(Array.isArray(position)) {
        return position.map(item => changeToLnglgt(item));
    } else {
        const ellipsoid = viewer.scene.globe.ellipsoid;
        const cartographic = ellipsoid.cartesianToCartographic(position);
        const lng = Cesium.Math.toDegrees(cartographic.longitude);
        const lat = Cesium.Math.toDegrees(cartographic.latitude);
        const height = cartographic.height;
        return [lng, lat, height];
    }
};

// 初始位置
const home = {
west: 73.33,
south: 3.51,
east: 135.05,
north: 53.33
};
// createPolyline(home)
// 成都范围位置
const chengdu = {
    west: 102.54,
    south: 30.05,
    east: 104.53,
    north: 31.26
};
// createPolygon(chengdu, 'blue', 0.5);
const sichuan = {
    west: 97.21,
    south: 26.03,
    east: 108.12,
    north: 34.19
};
// createPolygon(sichuan, 'blue', 0.1);
function flytoChengdu() {
    flyToPosition(chengdu);
};
function flytoSichuan() {
    flyToPosition(sichuan);
};
function flytoHome() {
    flyToPosition(home, 0.8, 'home');
}
flytoSichuan();

// 开启抗锯齿
viewer.scene.postProcessStages.fxaa.enabled = true;

// 取消默认的左键双击事件
viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
